package com.laijiale.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.laijiale.dto.CategoryInsertDTO;
import com.laijiale.dto.CategoryPageDTO;
import com.laijiale.dto.CategoryUpdateDTO;
import com.laijiale.exception.ServiceException;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.laijiale.entity.Category;
import com.laijiale.mapper.CategoryMapper;
import com.laijiale.service.CategoryService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import vo.CategorySimpleListVO;

import java.time.LocalDateTime;
import java.util.List;

import static com.laijiale.entity.table.CategoryTableDef.CATEGORY;


/**
 * 类别表 服务层实现。
 *
 * @author laijiale
 * @since 2024-12-22
 */
@Service
@CacheConfig(cacheNames = "category")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>  implements CategoryService{
    @CacheEvict(allEntries = true)
    @Override
    public boolean insert(CategoryInsertDTO dto) {
        //标题查重
        if(QueryChain.of(mapper).where(CATEGORY.TITLE.eq(dto.getTitle())).exists()){
            throw new ServiceException("类别标题已存在") ;

        }
        //组装entity实体类
        Category category= BeanUtil.copyProperties(dto,Category.class);
        category.setCreated(LocalDateTime.now());
        category.setUpdated(LocalDateTime.now());
        return mapper.insert(category)>0;

    }
    @Cacheable(key = "#p0",condition = "#p0 !=null",unless = "#result ==null")
    @Override
    public Category select(Long id) {
        return mapper.selectOneWithRelationsById(id);
    }

    @Cacheable(key = "#root.methodName",
            unless = "#result == null"
    )
    @Override
    public List<CategorySimpleListVO> simpleList() {
        List<CategorySimpleListVO> categorySimpleListVOS=QueryChain.of(mapper).listAs(CategorySimpleListVO.class);
        return categorySimpleListVOS;

    }

    @Cacheable(key = "#root.methodName + ':' + #p0.toString()",
            condition ="#p0 != null",
            unless = "#result == null")
            @Override
            public Page<Category> page(CategoryPageDTO dto) {
        QueryChain<Category> queryChain = QueryChain.of(mapper)
                .orderBy(CATEGORY.IDX.asc(), CATEGORY.ID.desc());

        // title 条件
        String title = dto.getTitle();
        if(ObjectUtil.isNotNull(title)) {
            queryChain.where(CATEGORY.TITLE.like(title));
        }
            return queryChain.withRelations().page(new Page<>(dto.getPageNum(), dto.getPageSize()));
        }


    @CacheEvict(allEntries = true)
    @Override
    public boolean update(CategoryUpdateDTO dto) {

        // 标题查重
        if (QueryChain.of(mapper).
                where(CATEGORY.TITLE.eq(dto.getTitle()))
                .and(CATEGORY.ID.ne(dto.getId()))
                .exists()) {
            throw new ServiceException("类别标题已存在");
        }

        // 组装 entity 实体类
        Category category = BeanUtil.copyProperties(dto, Category.class);
        // 设置 entity 默认值
        category.setUpdated(LocalDateTime.now());
        return UpdateChain.of(category).where(CATEGORY.ID.eq(category.getId())).update();
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean delete(Long id) {
        return UpdateChain.of(mapper).where(CATEGORY.ID.eq(id)).remove();
    }
    @CacheEvict(allEntries = true)
    @Override
    public boolean deleteBatch(List<Long> ids) {
        return UpdateChain.of(mapper).where(CATEGORY.ID.in(ids)).remove();
    }
    @Override
    @Cacheable(key = "#root. methodName",
            unless = "#result == null")
    public List<Category> list(){
    return QueryChain.of(mapper)
            .orderBy(CATEGORY.IDX.asc(), CATEGORY.ID.desc())
            .withRelations()
    .list();
    }

}
